package com.hadoop.controller;

import com.google.gson.Gson;
import com.hadoop.dao.redis.RedisDao;
import com.hadoop.mybatis.vo.SearchVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.Set;

/**
 * <p></p>
 *
 * @author Andy
 * @date 2017/10/19
 */
@RequestMapping(value = "/hadoop")
@Controller
public class SearchController {

    @Resource
    private RedisDao redisDao;


    @RequestMapping(value = "/search")
    public String search(SearchVo searchVo, Model model) {
        if (StringUtils.isNotBlank(searchVo.getText())) {
            model.addAttribute("searchData",redisDao.zRevRange(searchVo.getText(), 0, 5));
        }
        return "/search/result";
    }

    @RequestMapping(value = "/suggest")
    @ResponseBody
    public String suggest(SearchVo searchVo) {
        Gson gson = new Gson();
        String val = "";
        if (StringUtils.isNotBlank(searchVo.getQuery())) {
            Set<String> result = redisDao.zRevRange(searchVo.getQuery(), 0, 5);
            val = gson.toJson(result);
        }
        return val;
    }
}
